#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
    //printf("befor fork: I am a process, pid: %d, ppid: %d\n", getpid(), getppid());
    //sleep(5);
    //printf("开始创建进程\n");
    //sleep(1);
    //pid_t id = fork();
    //if(id < 0) return 1;
    //else if(id == 0) 
    //{
    //    printf("after fork: 子进程: pid: %d, ppid: %d\n", getpid(), getppid());
    //    sleep(1);
    //}
    //else 
    //{
    //     printf("after fork: 父进程: pid: %d, ppid: %d\n", getpid(), getppid());
    //     sleep(1);
    //}
    //sleep(2);


    //僵尸进程
    pid_t id = fork();
    if(id == 0)
    {
        int cnt = 5;
        while(cnt--)
        {
            printf("I am a child, pid: %d, ppid: %d\n", getpid(), getppid());
            sleep(1);
        }
        exit(0);
    }
    int cnt = 10;
    while(cnt--)
    {
        printf("I am a parent, pid: %d, ppid: %d\n", getpid(), getppid());
        sleep(1);
    }
    wait(NULL);
    printf("father process done\n");
    sleep(5);
    
    //孤儿进程
    //pid_t id = fork();
    //if(id == 0)
    //{
    //    int cnt = 500;
    //    while(cnt--)
    //    {
    //        printf("I am a child, pid: %d, ppid: %d\n", getpid(), getppid());
    //        sleep(1);
    //    }
    //    exit(0);
    //}
    //int cnt = 5;
    //while(cnt--)
    //{
    //    printf("I an a parent, pid: %d, ppid: %d\n", getpid(), getppid());
    //    sleep(1);
    //}
    return 0;
}

